/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.uah.es.snomedctsubsetextractor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Luis
 */
public class ConverterGlossarySnomed {

    public static void main(String[] args) {

        BufferedReader fstreamin = null;

        try {

            System.out.println("::::::::START - ConverterGlossarySnomed::::::::");
            //System.out.println("Args: " + args[0]);

            // Ruta donde se encuentra el fichero de configuraci�n de la base de datos.
            String pathdb = System.getProperty("user.dir") + File.separator + "etc" + File.separator + "confdb.xml";

            SNOMEDCTConfigurator DBConf = new SNOMEDCTConfigurator(pathdb);

            SNOMEDUtil util = new SNOMEDUtil(DBConf);

            LinkedList<Integer> conceptId = new LinkedList<Integer>();

            String path = System.getProperty("user.dir") + File.separator + "etc" + File.separator;
            //fstreamin = new BufferedReader(new FileReader(System.getProperty("user.dir") + File.separator + "etc" + File.separator + "Glossary-CancerOvario.txt"));
            fstreamin = new BufferedReader(new FileReader(path + args[0]));

            PrintStream out = new PrintStream(new FileOutputStream(path + args[0].substring(0, args[0].length() - 4) + "-Subset.txt"));

            PrintStream out2 = new PrintStream(new FileOutputStream(path + args[0].substring(0, args[0].length() - 4) + "-NO_Subset.txt"));

            // Get the object of DataInputStream
            String strLine;

            HashMap<Integer, String> concepts = new HashMap<Integer, String>();

            //Read File Line By Line
            while ((strLine = fstreamin.readLine()) != null) {
                // Print the content on the console
                conceptId.addAll(util.getConceptId(strLine));

                if (conceptId.isEmpty()) {
                    out2.println(strLine);
                } else {

                    while (!conceptId.isEmpty()) {
                        //concepts.add(
                        SNOMEDConcept concept = util.getSNOMEDConcept(conceptId.removeFirst());

                        int id = concept.getId();
                        String name = concept.getName();

                        if (!concepts.containsKey(id)) {
                            concepts.put(id, name);
                            out.println(id + ";" + name);
                        }

                    }
                }
            }

            //Close the input stream
            out.close();
            out2.close();

            System.out.println("::::::::THE END - ConverterGlossarySnomed::::::::");

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fstreamin.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
